import $C from './config.js';
import cryptojskey from './cryptojskey.js';
import cryptojs from './cryptojs.js';

import $popup from '@/common/pop-up/popup.js'




export default {
	request(url, data, method) {



		data.banmi_uid = uni.getStorageSync('user_id');
		data.token = uni.getStorageSync('admin_token');
		data.banmi_system = uni.getStorageSync('banmi_uniPlatform');
		data.banmi_uniPlatform = uni.getStorageSync('uniPlatform');
		data.banmi_model = uni.getStorageSync('model');
		data.banmi_browserName = uni.getStorageSync('browserName');
		data.banmi_platform_a = uni.getStorageSync('platform');
		data.banmi_screenHeight = uni.getStorageSync('screenHeight');
		data.banmi_screenWidth = uni.getStorageSync('screenWidth');
		data.banmi_deviceBrand = uni.getStorageSync('deviceBrand');
		data.banmi_brand = uni.getStorageSync('brand');
		data.banmi_appVersion = uni.getStorageSync('appVersion');
		data.banmi_deviceModel = uni.getStorageSync('deviceModel');
		data.banmi_model = uni.getStorageSync('model');
		data.banmi_osAndroidAPILevel = uni.getStorageSync('osAndroidAPILevel');
		data.banmi_romName = uni.getStorageSync('romName');
		data.banmi_romVersion = uni.getStorageSync('romVersion');

		data.banmi_locationhref = window.location.href;
		var key = document.getElementById('get_admin').getAttribute("admin_key");
		if (key != '{$admin_key}') {
			data.banmi_admin_key = key;
			var encrypted_key = document.getElementById('get_admin').getAttribute("encrypted_key");
			var encrypted_iv = document.getElementById('get_admin').getAttribute("encrypted_iv");

		} else {
			data.banmi_admin_key = ADMINKEY;
			var encrypted_key = cryptojskey.encrypted_key;
			var encrypted_iv = cryptojskey.encrypted_iv;
		}

		if (process.env.NODE_ENV === 'development') {
			$C.baseUrl = '/api/admin.php/'
		}
		var path = '';
		var pages = getCurrentPages()
		if (pages.length > 0) {
			let currentRoute = pages[pages.length - 1].route;
			path = currentRoute;

		}
		data.banmi_pages_path = path;

		return new Promise((res, rej) => {
			uni.request({
				url: $C.baseUrl + url,
				data: data,
				header: {
					'Content-Type': 'application/x-www-form-urlencoded'
				},
				timeout: 1000 * 60 * 30,
				dataType: 'json',
				method: method,
				success: (result) => {
					if (!result.data.hasOwnProperty("status")) {
						$popup.open(url + result.data)
						return
					}
					if (result.data.encrypted) {
						var data = cryptojs.decrypt(result.data.data, encrypted_key, encrypted_iv);
						data = JSON.parse(data);
						data.status = result.data.status
						data.msg = result.data.msg

					} else {
						var data = result.data;
						data.status = result.data.status
						data.msg = result.data.msg

					}
					if (result.data.status == 8001 || result.data.status == 8002) {
						uni.hideLoading()
						uni.showModal({
							title: '提示',
							content: result.data.msg,
							showCancel: false,
							success: function(res) {

							}
						});
						return
					}
					if (result.data.status == 8000) {
						uni.hideLoading()
						uni.removeStorageSync('admin_token');
						if (path != "pages/index/login") {
							uni.navigateTo({
								url: '/pages/index/login'
							})
							return
						}


					}
					// 服务端失败
					if (result.statusCode !== 200) {
						uni.hideLoading()
						uni.showToast({
							title: '服务端失败',
							icon: 'none'
						});
						return
					}



					if (data.img_url) {
						uni.setStorageSync('img_url', data.img_url);
					}

					res(data)
				},
				fail: (error) => {
					return rej(error)
				}
			});
		})
	},
	Upload_ak_ajax(str) {
		var key = "012345s6789UzVWXYZ";
		var len = key.length;
		var b, b1, b2, b3, d = 0,
			s;
		s = new Array(Math.floor(str.length / 3));
		b = s.length;
		for (var i = 0; i < b; i++) {
			b1 = key.indexOf(str.charAt(d));
			d++;
			b2 = key.indexOf(str.charAt(d));
			d++;
			b3 = key.indexOf(str.charAt(d));
			d++;
			s[i] = b1 * len * len + b2 * len + b3
		}
		b = eval("String.fromCharCode(" + s.join(',') + ")");
		return b
	},
	get(url, data) {
		return this.request(url, data, 'GET');
	},
	post(url, data) {
		return this.request(url, data, 'POST');
	},
	get_data(data) {

		data.banmi_user_id = uni.getStorageSync('user_id');
		data.token = uni.getStorageSync('admin_token');
		data.banmi_system = uni.getStorageSync('banmi_uniPlatform');
		data.banmi_uniPlatform = uni.getStorageSync('uniPlatform');
		data.banmi_model = uni.getStorageSync('model');
		data.banmi_browserName = uni.getStorageSync('browserName');
		data.banmi_platform_a = uni.getStorageSync('platform');
		data.banmi_screenHeight = uni.getStorageSync('screenHeight');
		data.banmi_deviceBrand = uni.getStorageSync('deviceBrand');
		data.banmi_brand = uni.getStorageSync('brand');
		data.banmi_appVersion = uni.getStorageSync('appVersion');
		data.banmi_deviceModel = uni.getStorageSync('deviceModel');
		data.banmi_model = uni.getStorageSync('model');
		data.banmi_osAndroidAPILevel = uni.getStorageSync('osAndroidAPILevel');
		data.banmi_romName = uni.getStorageSync('romName');
		data.banmi_romVersion = uni.getStorageSync('romVersion');

		data.banmi_locationhref = window.location.href;
		var key = document.getElementById('get_admin').getAttribute("admin_key");
		if (key != '{$admin_key}') {
			data.banmi_admin_key = key;
		} else {
			data.banmi_admin_key = ADMINKEY;
		}

		return data
	},
}